Continuous Delivery
Part 2, Chapter 12
In this chapter, we'll add continuous delivery (CD) to the existing GitHub Actions config.
First, retrieve your Heroku auth token:
$ heroku auth:token
Then, save the token as a new variable called HEROKU_AUTH_TOKEN within your repository's secrets (Settings > Secrets):
Next, add a new deploy job to the GitHub config file:
deploy:
name: Deploy to Heroku
runs-on: ubuntu-latest
needs: [build, test]
env:
HEROKU_APP_NAME: <APP_NAME>
HEROKU_REGISTRY_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/summarizer
steps:
- name: Checkout
uses: actions/checkout@v2.4.0
with:
ref: main
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.HEROKU_REGISTRY_IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Log in to the Heroku Container Registry
run: docker login -u _ -p ${HEROKU_AUTH_TOKEN} registry.heroku.com
env:
HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }}
- name: Push to the registry
run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }}
- name: Set environment variables
run: |
echo "HEROKU_REGISTRY_IMAGE=${{ env.HEROKU_REGISTRY_IMAGE }}" >> $GITHUB_ENV
echo "HEROKU_AUTH_TOKEN=${{ secrets.HEROKU_AUTH_TOKEN }}" >> $GITHUB_ENV
- name: Release
run: |
chmod +x ./release.sh
./release.sh
Make sure to replace
<APP_NAME>with your Heroku app's actual name.
Add release.sh to the project root:
#!/bin/sh
set -e
IMAGE_ID=$(docker inspect ${HEROKU_REGISTRY_IMAGE} --format={{.Id}})
PAYLOAD='{"updates": [{"type": "web", "docker_image": "'"$IMAGE_ID"'"}]}'
curl -n -X PATCH https://api.heroku.com/apps/$HEROKU_APP_NAME/formation \
-d "${PAYLOAD}" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.heroku+json; version=3.docker-releases" \
-H "Authorization: Bearer ${HEROKU_AUTH_TOKEN}"
In the deploy stage, we:
- Check out the repository so the job has access to it
- Log in to GitHub Packages
- Pull the image if it exists
- Build and tag the new image
- Log in to the Heroku Container Registry
- Push the image up to the registry
- Set the
HEROKU_REGISTRY_IMAGEandHEROKU_AUTH_TOKENenvironment variables withset-envso they can be accessed within the release file - Create a new release via the Heroku API using the image ID within the release.sh script
To test, make a quick change to the /ping GET route by removing the exclamation point (!):
@router.get("/ping")
async def pong(settings: Settings = Depends(get_settings)):
return {
"ping": "pong",
"environment": settings.environment,
"testing": settings.testing,
}
Update the test as well:
# project/tests/test_ping.py
def test_ping(test_app):
response = test_app.get("/ping")
assert response.status_code == 200
assert response.json() == {"environment": "dev", "ping": "pong", "testing": True}
Commit your code and again push it up to GitHub. Your app should be auto deployed to Heroku! Navigate to https://APP_NAME.herokuapp.com/ping/. You should now see:
{
"ping": "pong",
"environment": "dev",
"testing": false
}
✓ Mark as Completed